﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LitTools
{
    /// <summary>
    /// 使用PlayerPref进行存档
    /// 优点，不依赖任何第三方库
    /// 缺点，没有加密，不能序列化字典
    /// </summary>
    public class PlayerPrefSave : SaveBehaviour
    {
        public override T LoadData<T>()
        {
            T data = new T();
            if (PlayerPrefs.HasKey(data.SaveKey))
            {
                string value = PlayerPrefs.GetString(data.SaveKey);
                data = JsonUtility.FromJson<T>(value);
            }
            else
                data = data.GetDefaultValue();
            return data;
        }

        public override void SaveData<T>(T data)
        {
            string value = JsonUtility.ToJson(data);
            PlayerPrefs.SetString(data.SaveKey, value);
        }

        public override void WriteToDisk()
        {
            PlayerPrefs.Save();
        }
    }
}